package com.it.sun.yuebike.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * @ClassName com.it.sun.yuebike.framework.config.MyBatisConfig
 * @Author paopao
 * @Date 2021/5/31 9:45
 * @Version V1.0
 * Description  <请输入一句话描述该类功能>
 */
@Configuration
@MapperScan(basePackages = "com.it.sun.yuebike.**.mapper")
public class MyBatisConfig {

    /**
     * <描述方法功能>
     * <mybatis-plus属性填充策略>
     *
     * @param
     * @return com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
     * @Author paopao
     * @Date 2021/6/5 9:35
     **/
    @Bean
    public MetaObjectHandler metaObjectHandler() {

        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                if (metaObject.hasSetter("createTime")) {
                    metaObject.setValue("createTime", LocalDateTime.now());
                }
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                metaObject.setValue("updateDime", LocalDateTime.now());
            }
        };
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    @Bean
    public ISqlInjector sqlInjector() {
        return new DefaultSqlInjector();
    }

}



